const express = require("express");
const app = new express();
const path = require("path");

app.all("*", function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Content-Type");
  res.header("Access-Control-Allow-Methods", "*");
  res.header("Content-Type', 'application/json;charset=utf-8");
  next();
});
app.use(express.static(path.join(__dirname, "public")));

const userMsg = require("../db/userDB");

app.get("/login", async (req, res) => {
  const { query } = req;
  // console.log(query)
  const acount = query.acount || "";
  const pass = query.pass || "";
  console.log('这是acount:',acount)
  console.log('这是pass:',pass)
  // const pageIndex = query.pageIndex || "";
  const data = await userMsg.getUsers(acount, pass);
  // console.log(res)
  res.send({
    code: 0,
    data
  });
});

app.post("/login", async (req, res) => {
	const { query } = req
	const doc = query || ""
	console.log('这是doc:',doc)
	new userMsg(doc).save(function (err) {
		if(err) {
			return res.send("Server error")
		}
		// res.redirect("/login")
	})
})


app.listen(3000, function() {
  console.log("服务器启动成功了，可以通过http://127.0.0.1:3000访问");
});
